package com.earogya.logic.medical_records;

import java.util.Calendar;

import org.apache.struts.ehr.medical_records.model.ChangeConsultant;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.message.ADT_A61;

public class CreateADTA61 {

	private ADT_A61 adtMsg;
	private ChangeConsultant changeConsultant;
	
	public CreateADTA61(ChangeConsultant changeConsultant){
		adtMsg=new ADT_A61();
		this.changeConsultant=changeConsultant;
	}
	
	public String createMessage(String sendingFacility) throws HL7Exception{
		this.addMessageHeader(sendingFacility);
		this.addChangeConsultantDetails();
		return adtMsg.getMessage().encode();
	}
	
	private void addMessageHeader(String sendingFacility) throws DataTypeException{
		adtMsg.getMSH().getFieldSeparator().setValue("|");
		adtMsg.getMSH().getEncodingCharacters().setValue("^~\\&");
		adtMsg.getMSH().getDateTimeOfMessage().getTime().setValue(Calendar.getInstance().getTime());
		adtMsg.getMSH().getMessageType().getMsg1_MessageCode().setValue("ADT");
		adtMsg.getMSH().getMessageType().getTriggerEvent().setValue("A61");
		adtMsg.getMSH().getSendingApplication().getUniversalID().setValue("EAFE");
		adtMsg.getMSH().getVersionID().getVersionID().setValue("2.5.1");
		adtMsg.getMSH().getSendingFacility().getUniversalID().setValue(sendingFacility);
		adtMsg.getMSH().getReceivingApplication().getUniversalID().setValue("PVS");
		adtMsg.getMSH().getReceivingFacility().getUniversalID().setValue(sendingFacility);
	}
	
	private void addChangeConsultantDetails() throws DataTypeException{
		adtMsg.getPID().getPatientID().getIDNumber().setValue(String.valueOf(changeConsultant.getPatient_id()));
		adtMsg.getPV1().getConsultingDoctor(0).getIDNumber().setValue(String.valueOf(changeConsultant.getNew_consultant_id()));
	}
}
